<?php
/*
 * Tạo bởi truonghoanglinh@livisoft.com
 * 07/12/2011
 */
include_once 'config.php';

//Gọi chạy tự động
main();

//Kết nối CSDL
function connet_db(){
	global $db;
	if($db == null){
		if (!function_exists('gettext')) {
			function _($s) {return $s;}
		}
		include_once PATH_INCLUDES.'db'.DS.'ezsql'.DS.'mysql'.DS.'ez_sql_mysql.php';
		$db = new ezSQL_mysql(DB_USER, DB_PASSWORD, DB_DATABASE, DB_HOST);
		$db->cache_timeout = SQL_CACHE_TIME_OUT;
		$db->cache_dir = SQL_CACHE_DIR;
		$db->use_disk_cache = SQL_USE_DISK_CACHE;
		$db->cache_queries = SQL_CACHE_QUERIES;
		return true;
	}
	return true;
}

//Khởi tạo template engine smarty
function init_tpl(){
	global $tpl,$url_redirect,$routing,$session;
	$param = $routing->getParam();
	if($tpl == null){
		include_once PATH_INCLUDES.'smarty'.DS."Smarty.class.php";
		$tpl = new Smarty();
		$tpl->debugging = DEBUG;
		$tpl->caching = CACHE;
		$tpl->cache_lifetime = TIME_CACHE;
		$tpl->compile_dir = PATH_TEMPLATES_C;
		$tpl->plugins_dir = PATH_PLUGIN;
		$tpl->template_dir = PATH_TEMPLATES;
		$tpl->cache_dir = PATH_CACHE;
		$tpl->config_dir = ROOT_PATH;
		$tpl->assign('base_url',BASE_URL);
		$tpl->assign('base_uri',BASE_URI);
		$tpl->assign('current_page',CURRENT_PAGE);
		$tpl->assign('module',$param[1]);
		$tpl->assign('path_templates',BASE_PATH_TEMPLATES);
		$tpl->assign('path_css',BASE_PATH_CSS);
		$tpl->assign('path_js',BASE_PATH_JS);
		$tpl->assign('path_images',BASE_PATH_IMAGES);
		$tpl->assign('user_name',$session->auth_username);
		$tpl->assign('query_string_full',QUERY_STRING_FULL);
		return true;
	}
	return true;

}

function init_auth(){
	global $auth;
	include_once PATH_MODELS.'auth'.SUFFIX;
	if($auth == null){
		$auth = new Auth();
		return true;
	}
	return true;
}

function write_log(){
	global $session;
	if(isset($session->auth_username)){
		$user = $session->auth_username;
	}else{
		$user = 'guest';
	}
	$file = PATH_LOGS.$user.'_'.strtotime(date('d-m-Y')).'.log';
	$date = date("d-m-Y H:i:s"); //Get the date and time.
	$open = fopen($file, "a+"); //open the file, (log.htm).
	fwrite($open, $date.'|'.IP.'|'.USER_AGENT.'|'.QUERY_STRING_FULL.'
');
	fclose($open);
}

function log_error(){
	//error_reporting(E_ALL);
	//ini_set("display_errors", 1);
}

function init_config(){
	ini_set('memory_limit', MENMORY_LIMIT);
}

/**
 * Kiểm tra đăng nhập
 */
function check_login(){
	global $url_redirect,$auth,$routing;
	$param = $routing->getParam();
	if(!$auth->locked === true){
		$query = (strpos(QUERY_STRING_FULL, 'login') === false) ? 'admin-login/display/return/'.$param[1] : 'admin-login';
		redirect(BASE_URI.$query);
	}
}


/**
 * Hightlight lỗi
 * @param array | object $data
 * @param bool $flag
 */
function pre_data($data,$flag=false){
	echo '<pre>';
	var_dump($data);
	echo '</pre>';
	if($flag) die;
}

/**
 * Khởi tao session đếm lượt đăng nhập
 * chống đăng nhập tự đông
 */
function init_count(){
	global $session;
	if(!isset($session->count)){
		$session->count = 3;
	}
}

/**
 * Khởi tạo sesion
 * @return boolean
 */
function init_session(){
	global $session;
	if($session == null){
		include_once PATH_INCLUDES.'application'.DS."Session.php";
		$session = Session::getInstance();
		return true;
	}
	return false;
}

/**
 * Lấy URL hiện taij
 * @param bool $lowercase
 * @return string
 */
function get_current_page($lowercase = false) {
	$pageURL = 'http';
	if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
	$pageURL .= "://";
	if ($_SERVER["SERVER_PORT"] != "80") {
		$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
	} else {
		$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
	}
	return ($lowercase ? strtolower($pageURL) : $pageURL);
}

/**
 * Chuyển tiếp url
 * @param string $url
 */
function redirect($url){
	@header('Location: '.$url);
	exit;
}

/**
 * Tự động thêm tham số lang vào url
 */
function set_default_lang(){
	global $session;
	if(!isset($session->lang)){
		$session->lang = 'vi';
	}
}

/**
 * Chuyển tiếp nếu chưa có tham số lang
 */
function redirect_null_param_lang(){
	global $session,$routing;
	$param = $routing->getParam();
	if(isset($session->lang) && $session->lang != $param[0]){
		redirect(BASE_URL.$session->lang.DS.QUERY_STRING);
	}
}

/**
 * Khởi tạo url routing
 * @return boolean
 */
function init_routing(){
	global $routing;
	if($tpl == null){
		include_once PATH_INCLUDES.'application'.DS."Routing.php";
		$routing = Routing::getInstance();
		return true;
	}
	return false;
}

/**
 * Khởi tạo một số hằng mặc định
 */
function define_default(){
	global $session;
	include_once PATH_INCLUDES.'application'.DS."Routing.php";
	define('BASE_URI', BASE_URL.$session->lang.DS);
	define('CURRENT_PAGE', get_current_page());
}

/**
 * Chuyển đổi đơn vị
 * @param int $size
 * @return string
 */
function convert($size)
{
	$unit=array('B','KB','MB','GB','TB','PB');
	return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}

function debug(){
	global $debug,$tpl,$session;
	include_once PATH_INCLUDES.'debug'.DS."debug.php";
	$debug = debug::getInstance();
	static $output = '', $doc_root;
	$tpl->assign('view_debug',VIEW_DEBUG);
	if(VIEW_DEBUG){
		$tpl->assign('debug_menmory',convert(memory_get_usage(true)));
		$tpl->assign('debug_session',$_SESSION);
		$tpl->assign('debug_cookies',$_COOKIE);
		$tpl->assign('debug_post',$_POST);
		$tpl->assign('debug_get',$_GET);
		$tpl->assign('debug_backtrace',debug_backtrace());
	}
}

//Gọi hàm
function main(){
	global $url_redirect;
	init_routing();
	init_session();
	set_default_lang();
	redirect_null_param_lang();
	define_default();
	init_tpl();
	debug();
	connet_db();
	init_auth();
	init_count();
	write_log();
}

?>